package com.lanyuan.controller.sys;

import com.lanyuan.annotation.SystemLog;
import com.lanyuan.controller.index.BaseController;
import com.lanyuan.entity.sys.SysConfigFormMap;
import com.lanyuan.exception.SystemException;
import com.lanyuan.mapper.sys.SysConfigMapper;
import com.lanyuan.plugin.PageView;
import com.lanyuan.util.Common;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.inject.Inject;

@Controller
@RequestMapping("/sys/config/")
public class SysConfigController extends BaseController {

    @Inject
    private SysConfigMapper sysConfigMapper;

    @RequestMapping("list")
    public String listUI(Model model) throws Exception {
        model.addAttribute("res", findByRes());
        return Common.BACKGROUND_PATH + "/sys/config/list";
    }

    @ResponseBody
    @RequestMapping("findByPage")
    public PageView findByPage(String pageNow, String pageSize, String column, String sort) throws Exception {
        SysConfigFormMap formMap = getFormMap(SysConfigFormMap.class);
        formMap = toFormMap(formMap, pageNow, pageSize, formMap.getStr("orderby"));
        formMap.put("column", column);
        formMap.put("sort", sort);
        PageView pageView = getPageView(formMap);
        pageView.setRecords(sysConfigMapper.findPage(formMap)); // 不调用默认分页,调用自已的mapper中findPage
        return pageView;
    }

    @RequestMapping("addUI")
    public String addUI(Model model) throws Exception {
        return Common.BACKGROUND_PATH + "/sys/config/add";
    }

    @ResponseBody
    @RequestMapping("addEntity")
    @SystemLog(module = "后台管理", methods = "配置管理-新增配置") // 凡需要处理业务逻辑的.都需要记录操作日志
    @Transactional(readOnly = false) // 需要事务操作必须加入此注解
    public String addEntity() {
        try {
            SysConfigFormMap formMap = getFormMap(SysConfigFormMap.class);
            sysConfigMapper.addEntity(formMap); // 新增后返回新增信息
        } catch (Exception e) {
            throw new SystemException("添加配置异常");
        }
        return "success";
    }

    @RequestMapping("editUI")
    public String editUI(Model model) {
        String id = getPara("id");
        if (Common.isNotEmpty(id)) {
            SysConfigFormMap formMap = sysConfigMapper.findOne("id", id, SysConfigFormMap.class);
            model.addAttribute("sysConfig", formMap);
        }
        return Common.BACKGROUND_PATH + "/sys/config/edit";
    }

    @ResponseBody
    @RequestMapping("editEntity")
    @Transactional(readOnly = false) // 需要事务操作必须加入此注解
    @SystemLog(module = "系统管理", methods = "配置管理-修改配置信息") // 凡需要处理业务逻辑的.都需要记录操作日志
    public String editEntity() throws Exception {
        String code = "success";
        SysConfigFormMap formMap = getFormMap(SysConfigFormMap.class, false);
        sysConfigMapper.editEntity(formMap);
        return code;
    }

    @ResponseBody
    @RequestMapping("deleteEntity")
    @Transactional(readOnly = false) // 需要事务操作必须加入此注解
    @SystemLog(module = "系统管理", methods = "配置管理-删除配置") // 凡需要处理业务逻辑的.都需要记录操作日志
    public String deleteEntity() throws Exception {
        String[] ids = getParaValues("ids");
        for (String id : ids) {
            sysConfigMapper.deleteByAttribute("id", id, SysConfigFormMap.class);
        }
        return "success";
    }

}
